package com.bimowu.first.service.impl;

import com.bimowu.first.dao.ProductDao;
import com.bimowu.first.model.Product;
import com.bimowu.first.service.ProductService;
import com.bimowu.first.utils.bean.CommonQueryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Override
    public List<Product> listShowProduct() {
        Product record = new Product();
        record.setState(1);
        return productDao.list(record);
    }
    @Override
    public Product findByProductId(long productId) {
        Assert.notNull(productId, "productId is null!");
        Product record = new Product();
        record.setProductId(productId);
        List<Product> list = productDao.list(record);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Product> list4Page(Product record, CommonQueryBean query) {
        return productDao.list4Page(record, query);
    }

    @Override
    public long count(Product record) {
        return productDao.count(record);
    }

    @Override
    public int save(Product product) {
        Assert.notNull(product.getProductId(),"id不能为空");
        Assert.notNull(product.getName(),"商品名称不能为空");
        Product param = new Product();
        param.setProductId(product.getProductId());
        long res = count(param);
        if(res > 0){
            return -1;
        }
        Date date = new Date();
        product.setCreateTime(date);
        product.setStartShowTime(date);
        product.setState(0);
        return  productDao.insert(product);
    }

    @Override
    public Product selectByPrimaryKey(Long id) {
        return productDao.selectByPrimaryKey(id);
    }

    @Override
    public void updateItem(Product product) {
        Assert.notNull(product.getId(),"id不能为空");
        Product record = new Product();
        record.setId(product.getId());
        record.setName(product.getName());
        record.setDesc(product.getDesc());
        record.setImgUrl(product.getImgUrl());
        productDao.updateByPrimaryKeySelective(product);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return productDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<Product> listByOrderNo(String orderNo) {
        Assert.notNull(orderNo, "orderNo is not null!");
        return productDao.listByOrderNo(orderNo);
    }

}
